// pages/operat/fleedTools/inStorage/inStorage.js
const LApp = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
  inStorageModel: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   let data = LApp.pages.get("fleedTools/inStorage/scanCode/scanCode").data;
   this.setData({
    inStorageModel: data.StorageInfo
   })
   if (this.data.inStorageModel.ProductID != 0) {
    this.productShow(this.data.inStorageModel.ProductID);
   }
   else
    this.getproductList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  this.GetFormCode(0);
   this.getWarehouseList();
   this.getproductList();
   
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  GetFormCode:function(data){
    var formCode = LApp._formatTime("yyyyMMddhhmmss");
    if (data){
      formCode = data + "-" + formCode;
    }
    this.data.inStorageModel.FormCode = formCode;
    this.setData(this.data);
  },
 
//仓库下拉框
  getWarehouseList: function () {
   let _this = this;
   LApp._get({
    url: 'nbapi/warehouse/wselectitem',
    data: { userId: LApp.globalData.userInfo.UserID },
    success: function (r) {
     _this.setData({
      warehouseList: r
     })
     
    }
   })
  },
  warehouseSelect: function (e) {
   var _warehouse = this.data.warehouseList[e.detail.value];
   if (_warehouse) {
    this.data.inStorageModel.wName = _warehouse.Name;
    this.data.inStorageModel.WarehouseId = _warehouse.Code;
    this.setData(this.data);
    var proId = this.data.inStorageModel.ProductID;
     proId=proId == undefined ? 0 : proId;
    this.GetFormCode(proId+ "-" + _warehouse.Code);
   }
  },
  //产品下拉框
  getproductList: function () {
   let _this = this;
   LApp._get({
    url: 'nbapi/product/pselectitem',
    data: { userId: LApp.globalData.userInfo.UserID },
    success: function (r) {
     _this.setData({
      productList: r
     })
    }
   })
  },
  productSelect: function (e) {
   var _product = this.data.productList[e.detail.value];
   if (_product) {
    this.data.inStorageModel.pName = _product.Name;
    this.data.inStorageModel.ProductID = _product.Code;
    this.setData(this.data);
    var wId = this.data.inStorageModel.WarehouseId
    wId=wId == undefined ? 0 : wId;
    this.GetFormCode(_product.Code + "-" + wId);
   }
  
  }, 

  productShow: function (proId) {
   let _this = this;
   LApp._get({
    url: 'nbapi/product/showproductinfo',
    data: { proId: proId },
    success: function (res) {
     _this.data.inStorageModel.pName = res.ProductName;
     _this.data.inStorageModel.ProductID = res.Id;
     _this.setData(_this.data);
    }
   })
  },

  bindModelChange: function (e) {
   wx.$$apply(e, this.data.inStorageModel);
  },
  saveStorag: function () {
 
   if (this.data.inStorageModel.ProductID == 0 || this.data.inStorageModel.ProductID == undefined ) {
    wx.showModal({
     title: '提示',
     content: '请选择产品名称',
     showCancel: false
    })
    return;
   }
   else if (this.data.inStorageModel.WarehouseId == undefined || this.data.inStorageModel.WarehouseId == 0){
    wx.showModal({
     title: '提示',
     content: '请选择出货仓库！',
     showCancel: false
    })
    return;

   }
   else{
   this.data.inStorageModel.FormType = 1;
   this.data.inStorageModel.CreateLoginID = LApp.globalData.userInfo.UserID;
   this.data.inStorageModel.CreateLoginName = LApp.globalData.userInfo.LoginName;
   this.data.inStorageModel.EnterpriseID = LApp.globalData.userInfo.EnterpriseId;
  // var a= this.data.inStorageModel;
   LApp._post({
    url: 'nbapi/Storage/pinStorage/add',
    data: this.data.inStorageModel,
    success: function (r) {
      wx.showModal({
        title: '保存成功！',
        icon: "success",
        success: function (res) {
          if (!res.cancel) {
            var pages = getCurrentPages();
            var currPage = pages[pages.length - 1];   //当前页面
            var prevPage = pages[pages.length - 2];  //上一个页面
            prevPage.setData({
              myDate: 1
            });
            wx.navigateBack(1);
          }
        }
      })
    }
   })
  }
  },
  goSearchCode: function () {
   if (this.data.inStorageModel) {
    LApp.pages.add(this);
   wx.navigateTo({
    url: 'searchCode/searchCode',
   })
  }
  }
  
}) 